/**
 * 
 */
package com.ws.framework.model;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.google.common.base.Joiner;
import com.google.common.collect.Maps;

/**
 * <p>
 * </p>
 *
 * @author WSH
 */
public class Page<T> implements Serializable {

	private static final long serialVersionUID = -1942354641324974685L;

	// 页面大小
	private Integer pageSize;

	// 页码
	private Integer pageNumber;
	
	//第几页
	private Integer current;

	private Long total;

	private Map<String, Object> condition;

	private List<T> rows;

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(Integer pageNumber) {
		this.pageNumber = pageNumber;
	}

	public Long getTotal() {
		return total;
	}

	public void setTotal(Long total) {
		this.total = total;
	}

	public String getAllEqCondition() {
		if (Objects.isNull(condition) || condition.isEmpty())
			return "";
		return " AND " + Joiner.on(" AND ").withKeyValueSeparator(" = ").join(condition);
	}

	public String getAllLikeCondition() {
		if (Objects.isNull(condition) || condition.isEmpty())
			return "";
		return " AND " + Joiner.on(" AND ").withKeyValueSeparator(" LIKE ").join(condition);
	}

	public String getSomeLikeOtherEqCondition(String... likeKeys) {
		if (Objects.isNull(condition) || condition.isEmpty())
			return "";
		Map<String, Object> likeMap = Maps.newHashMap();
		for (String key : likeKeys) {
			Object val = condition.remove(key);
			if (!Objects.isNull(val))
				likeMap.put(key, val);
		}
		if (likeMap.isEmpty() && condition.isEmpty()) {
			return "";
		} else if (likeMap.isEmpty() && !condition.isEmpty()) {
			return getAllEqCondition();
		} else if (!likeMap.isEmpty() && condition.isEmpty()) {
			condition = likeMap;
			return getAllLikeCondition();
		} else 
			return " AND " + Joiner.on(" AND ").join(Joiner.on(" AND ").withKeyValueSeparator(" = ").join(condition),
					Joiner.on(" AND ").withKeyValueSeparator(" LIKE ").join(likeMap));
	}

	public void setCondition(Map<String, Object> condition) {
		this.condition = condition;
	}

	public List<T> getRows() {
		return rows;
	}

	public void setRows(List<T> rows) {
		this.rows = rows;
	}

	public Integer getCurrent() {
		return current;
	}

	public void setCurrent(Integer current) {
		this.current = current;
	}
	
	private String getLimitSql() {
		return " LIMIT " + (current-1)*pageSize + "," + pageSize ;
	}
	
	public String getCountSql(String sql) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT COUNT(1) AS COUNT FROM (");
		sb.append(sql);
		sb.append(") countAliasU");
		return sb.toString();
	}
	
	public Page<T> get(String sql, JdbcTemplate tempalte, Class<T> clazz) {
		Map<String, Object> map = tempalte.queryForMap(getCountSql(sql));
		List<T> list = tempalte.query(sql + getLimitSql(), new BeanPropertyRowMapper<T>(clazz));
		this.setRows(list);
		this.setTotal((Long)map.get("COUNT"));
		return this;
	}
	
}
